package com.czkt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czkt.config.MyRealm;
import com.czkt.pojo.BasDict;
import com.czkt.pojo.SysRole;
import com.czkt.pojo.SysRoleRight;
import com.czkt.service.IBasDictService;
import com.czkt.util.PageUtile;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liu
 * @since 2024-06-21
 */
@Controller
@RequestMapping("/dict")
public class BasDictController {


    @Autowired
    IBasDictService basDictService;

    @RequestMapping("/list")
    public Object getCustomerTypeList(BasDict basDict, @RequestParam(defaultValue = "1") Integer pageIndex,@RequestParam(defaultValue = "2") Integer pageSize, Model model){

        IPage<BasDict> page = new Page<>();
        page.setCurrent(pageIndex);
        page.setSize(pageSize);


        QueryWrapper<BasDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BasDict::getDictType,"客户等级");
        queryWrapper.lambda().eq(basDict.getDictId()!=null,BasDict::getDictId,basDict.getDictId());
        queryWrapper.lambda().like(StringUtils.hasText(basDict.getDictItem()),BasDict::getDictItem,basDict.getDictItem());

        page= basDictService.page(page,queryWrapper);

        PageUtile pageUtile = new PageUtile();

        BeanUtils.copyProperties(page,pageUtile);

        model.addAttribute("dictId",basDict.getDictId());
        model.addAttribute("dictItem",basDict.getDictItem());
        model.addAttribute("dictPage",pageUtile);
        return "dict/list";
    }


    /**
     * 定义跳转到新增页的方法
     */
    @RequestMapping("/add")
    public Object toAdd(Model model) {

        return "dict/add";
    }

    /**
     * 定义跳转到新增页的方法
     */
    @RequestMapping("/save")
    @Transactional
    public Object saveOrUpdate(BasDict basDict,  Model model) {

        boolean result = basDictService.saveOrUpdate(basDict);
        if (result) {


            return "redirect:/dict/list";
        } else if (basDict.getDictId() == 0) {
            return "redirect:/dict/add";
        } else {
            return "redirect:/dict/edit?usrId=" + basDict.getDictId();
        }

    }


    /**
     * 定义跳转到新增页的方法
     */
    @RequestMapping("/edit")
    public Object toEdit(long dictId, Model model) {
        model.addAttribute("dict", basDictService.getById(dictId));

        return "dict/edit";
    }



}
